package com.rd.sys.common.web.menu;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.rd.sys.common.utils.GetWebConfig;

/**
 * The Class Menu.
 */
public final class Menu {

    private static final String MENU_TYPE = "eye";

    /** The menu. */
    private static Menu menu = new Menu();

    /** The groups. */
    @SuppressWarnings("rawtypes")
    private final List groups = new ArrayList();

    /**
     * Instantiates a new menu.
     */
    private Menu() {
        try {
            getItems();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the single instance of Menu.
     * 
     * @return single instance of Menu
     */
    public static Menu getInstance() {
        return menu;
    }

    /**
     * Gets the items.
     * 
     * @return the items
     * @throws DocumentException
     *             f
     * @throws MalformedURLException
     *             f
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    private void getItems() throws DocumentException, MalformedURLException {
        Document document = getDocument();
        Element rootElement = document.getRootElement();
        if (MENU_TYPE.equals(rootElement.attributeValue("type"))) {
            Iterator tabs = rootElement.elementIterator();
            while (tabs.hasNext()) {
                List tab = new ArrayList();
                groups.add(tab);

                Element tabElement = (Element) tabs.next();
                addElementsToGroups(tab, tabElement);
            }
        } else {
            addElementsToGroups(groups, rootElement);
        }
    }

    /**
     * Adds the elements to groups.
     * 
     * @param groups
     *            f
     * @param element
     *            f
     * @version
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    private void addElementsToGroups(List groups, Element element) {
        Iterator xmlgroups = element.elementIterator();
        while (xmlgroups.hasNext()) {
            // 一级菜单
            Element xmlgroup = (Element) xmlgroups.next();
            Group group = new Group();
            group.setName(xmlgroup.attributeValue("name").trim());
            group.setIconClass(xmlgroup.attributeValue("class").trim());
            Iterator childgroups = xmlgroup.elementIterator();
            List childgroupList = new ArrayList();

            while (childgroups.hasNext()) {
                // 二级菜单
                Element childGroupElement = (Element) childgroups.next();
                ChildGroup childGroup = new ChildGroup();
                String name = childGroupElement.attributeValue("name");
                childGroup.setName(name);

                if (childGroupElement.attributeValue("url") == null) {
                    Iterator items = childGroupElement.elementIterator();
                    List itemList = new ArrayList();
                    while (items.hasNext()) {
                        // 三级菜单
                        Element itemElement = (Element) items.next();
                        String item_name = itemElement.attributeValue("name");
                        String item_url = itemElement.attributeValue("url").trim();
                        String item_id = itemElement.attributeValue("id").trim();
                        Item item = new Item(item_name, item_url, item_id);
                        itemList.add(item);
                    }
                    childGroup.setItems(itemList);
                } else {
                    childGroup.setUrl(childGroupElement.attributeValue("url"));
                    childGroup.setId(childGroupElement.attributeValue("id"));
                }
                childgroupList.add(childGroup);

            }
            if (childgroupList.size() > 0) {
                group.setChildgroup(childgroupList);
                groups.add(group);
            }
        }
    }

    /**
     * Gets the document.
     * 
     * @return f
     * @throws DocumentException
     *             f
     * @throws MalformedURLException
     *             f
     */
    private Document getDocument() throws DocumentException, MalformedURLException {
        String menuconfig = GetWebConfig.getValue("menutype");
        URL menuUrl = Menu.class.getResource(menuconfig);
        File menuFile = new File(menuUrl.getFile());
        return new SAXReader().read(menuFile);
    }

    /**
     * Gets the groups.
     * 
     * @return f
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List getGroups() {
        List cloneGroups = new ArrayList();
        cloneGroups.addAll(groups);

        return cloneGroups;
    }
}
